#include "UsrAI.h"
#include<set>
#include <iostream>
#include<unordered_map>
#include<vector>
using namespace std;
tagGame tagUsrGame;
ins UsrIns;
//这次要努力考虑算法问题
//声明
tagInfo info;
int stage = -1;

char mymap[128][128];

int home_x = -1, home_y = -1;
int dock1_x = -1, dock1_y = -1;
int dock2_x = -1, dock2_y = -1;
int stock_x = -1, stock_y = -1;
int granary_x = -1, granary_y = -1;
int market_x = -1, market_y = -1;
int farm_x = -1, farm_y = -1;

std::vector<tagArmy>clubman;//棍棒兵
std::vector<tagArmy>slinger;//投石兵
std::vector<tagArmy>bowman;//弓兵
std::vector<tagArmy>scout;//侦察骑兵
std::vector<tagArmy>swordsman;//战斧兵
std::vector<tagArmy>ship;//战船
std::vector<tagArmy>idleship;//空闲战船

std::vector<tagFarmer>idleFarmer;               // 存储闲置的农民
std::vector<tagFarmer>sailing;                  //渔船
std::vector<tagFarmer>idleSailing;               //空闲渔船
std::vector<tagFarmer>boat;                  //运输船
std::vector<tagFarmer>idleBoat;               //空闲运输船

std::vector<tagFarmer>fishingFarmer;            // 存储捕鱼的农民
std::vector<tagFarmer>huntingFarmer_ele;            // 存储猎大象的农民
std::vector<tagFarmer>huntingFarmer_ga;            // 存储猎瞪羚的农民
std::vector<tagFarmer>cuttingFarmer;            // 存储砍树的农民
std::vector<tagFarmer>choppingFarmer;            // 存储砍石头的农民

std::vector<tagFarmer>buildingHouseFarmer;      // 存储建造房子的农民
std::vector<tagFarmer>buildingGranaryFarmer;      // 存储建造谷仓的农民
std::vector<tagFarmer>buildingStockFarmer;      // 存储建造仓库的农民
std::vector<tagFarmer>buildingFarmFarmer;      // 存储建造农田的农民
std::vector<tagFarmer>buildingMarketFarmer;      // 存储建造市场的农民
std::vector<tagFarmer>buildingArrowtowerFarmer;      // 存储建造箭塔的农民
std::vector<tagFarmer>buildingArmycampFarmer;      // 存储建造兵营的农民
std::vector<tagFarmer>buildingStableFarmer;      // 存储建造马厩的农民
std::vector<tagFarmer>buildingRangeFarmer;      // 存储建造靶场的农民
std::vector<tagFarmer>buildingDockFarmer;      // 存储建造船坞的农民

std::map<int,tagResource> SN_to_Resource;       //SN与资源的映射关系
std::map<int,tagBuilding> SN_to_Building;       //SN与建筑的映射关系
std::map<int,tagFarmer> SN_to_Farmer;       //SN与农夫的映射关系
std::map<int, bool> SN_isOccupied;             //通过SN查看资源是否被占用

std::vector<tagResource>fish;            // 鱼场
std::vector<tagResource>liveele;            // 活大象
std::vector<tagResource>deadele;            // 死大象
std::vector<tagResource>livega;            // 活瞪羚
std::vector<tagResource>deadga;            // 死瞪羚
std::vector<tagResource>tree;            // 树
std::vector<tagResource>stone;            // 石头

std::vector<tagBuilding> home;  //房子
std::vector<tagBuilding> granary; //谷仓
std::vector<tagBuilding> mycenter; //市镇中心
std::vector<tagBuilding> stock;  //仓库
std::vector<tagBuilding> farm;  //农田
std::vector<tagBuilding> market ;//市场
std::vector<tagBuilding> arrowtower; //箭塔
std::vector<tagBuilding> armycamp; //兵营
std::vector<tagBuilding> stable; //马厩
std::vector<tagBuilding> range; //靶场
std::vector<tagBuilding> dock; //船坞

tagFarmer hunter1 , hunter2; //猎瞪羚
tagFarmer hunter3 ;  //猎大象
tagResource hunting_ele ;
int eleflag = 0;

int livega_num;//活瞪羚数量
int liveele_num;//活大象
int ele_blood = 45;
int tree_flag = 0;

//函数部分
//非class

//查找距离某农民最近的资源
double caldis(int x1, int y1, int x2, int y2)
{
    double x = x1 - x2;
    double y = y1 - y2;
    return (sqrt(x * x + y * y));
}
tagResource NearestResource(vector<tagResource> res, tagFarmer farmer)
{
    tagResource nearest;
    double minDis = 1e6;
    double tmpDis;
        for (tagResource resource : res)
        {
            if (SN_isOccupied[resource.SN] == false)
            {
                tmpDis = caldis( farmer.BlockDR, farmer.BlockUR, resource.BlockDR, resource.BlockUR);
                if (tmpDis < minDis)
                {
                    minDis = tmpDis;
                    nearest = resource;
                }
            }
        }
    return nearest;
}

void setAll() //初始化
{
    //因为vector机制全部清空
    clubman.clear();//棍棒兵
    slinger.clear();//投石兵
    bowman.clear();//弓兵
    scout.clear();//侦察骑兵
    swordsman.clear();//战斧兵
    ship.clear();//战船
    idleship.clear();//空闲战船

    idleFarmer.clear();
    sailing.clear();                  //渔船
    idleSailing.clear();
    boat.clear();                  //运输船
    idleBoat.clear();               //空闲运输船

    fishingFarmer.clear();
    huntingFarmer_ele.clear();
    huntingFarmer_ga.clear();
    cuttingFarmer.clear();
    choppingFarmer.clear();


    buildingHouseFarmer.clear();      // 存储建造房子的农民
    buildingGranaryFarmer.clear();      // 存储建造谷仓的农民
    buildingStockFarmer.clear();      // 存储建造仓库的农民
    buildingFarmFarmer.clear();     // 存储建造农田的农民
    buildingMarketFarmer.clear();    // 存储建造市场的农民
    buildingArrowtowerFarmer.clear();     // 存储建造箭塔的农民
    buildingArmycampFarmer.clear();      // 存储建造兵营的农民
    buildingStableFarmer.clear();      // 存储建造马厩的农民
    buildingRangeFarmer.clear();     // 存储建造靶场的农民
    buildingDockFarmer.clear();      // 存储建造船坞的农民

    fish.clear();            // 鱼场
    liveele.clear();            // 大象
    deadele.clear();
    livega.clear();            // 活瞪羚
    deadga.clear();            // 死瞪羚
    tree.clear();            // 树
    stone.clear();            // 石头

    home.clear();  //房子
    granary.clear(); //谷仓
    mycenter.clear(); //市镇中心
    stock.clear();  //仓库
    farm.clear();  //农田
    market.clear(); ;//市场
    arrowtower.clear(); //箭塔
    armycamp.clear(); //兵营
    stable.clear(); //马厩
    range.clear(); //靶场
    dock.clear(); //船坞

    //大致地域 O(128x128)
    for (int i = 0; i < 128; i ++)
    {
        for (int j = 0; j < 128; j ++)
        {
            if ((*info.theMap)[i][j].type == MAPPATTERN_GRASS)
            {
                int h = (*info.theMap)[i][j].height;
                if (j > 0 && (*info.theMap)[i][j - 1].height != h
                        && mymap[i][j - 1] != '~')
                {
                    mymap[i][j] = '!';//斜坡
                    mymap[i][j - 1] = '!';
                }
                else if (i > 0 && (*info.theMap)[i - 1][j].height != h
                         && mymap[i - 1][j] != '~')
                {
                    mymap[i][j] = '!';
                    mymap[i - 1][j] = '!';
                }
                else
                {
                    mymap[i][j] = '0' + (*info.theMap)[i][j].height; //空地+其高度
                }
            }
            if ((*info.theMap)[i][j].type == MAPPATTERN_UNKNOWN)
                mymap[i][j] = '-'; //未知地区
            if ((*info.theMap)[i][j].type == MAPPATTERN_OCEAN)
                mymap[i][j] = '~'; //海洋
        }
    }

    Globalmanage Coverarea;
    Coverarea.TraverseResource();
    Coverarea.TraverseBuilding();
    Coverarea.TraverseFarmer();
    Coverarea.TraverseArmy();
}

bool findOpenspace(int type, int begin_x, int begin_y, int dr, int ur, int &target_x, int &target_y)
{
    if (type == 1)
    {
        for (int i = begin_x; i >= 0 ; i --)
        {
            for (int j = begin_y; j < 128; j ++)
            {
                bool valid = true;

                for (int x = 0; x < dr && valid; x ++)
                {
                    for (int y = 0; y < ur && valid; y ++)
                    {
                        if (mymap[i+x][j+y] < '0' or
                                mymap[i+x][j+y] > '3') valid = false;
                    }
                }

                if (valid)
                {
                    target_x = i;
                    target_y = j;
                    return true;
                }
            }
        }
        return false;
    }
    if (type == 2)
    {
        for (int i = begin_x; i < 128 ; i ++)
        {
            for (int j = begin_y; j < 128; j ++)
            {
                bool valid = true;

                for (int x = 0; x < dr && valid; x ++)
                {
                    for (int y = 0; y < ur && valid; y ++)
                    {
                        if (mymap[i+x][j+y] < '0' or
                                mymap[i+x][j+y] > '3') valid = false;
                    }
                }

                if (valid)
                {
                    target_x = i;
                    target_y = j;
                    return true;
                }
            }
        }
        return false;
    }
    if (type == 3)
    {
        for (int i = begin_x; i >= 0 ; i --)
        {
            for (int j = begin_y; j >= 0; j --)
            {
                bool valid = true;

                for (int x = 0; x < dr && valid; x ++)
                {
                    for (int y = 0; y < ur && valid; y ++)
                    {
                        if (mymap[i+x][j+y] < '0' or
                                mymap[i+x][j+y] > '3') valid = false;
                    }
                }

                if (valid)
                {
                    target_x = i;
                    target_y = j;
                    return true;
                }
            }
        }
        return false;
    }
    if (type == 4)
    {
        for (int i = begin_x; i < 128 ; i ++)
        {
            for (int j = begin_y; j >= 0; j --)
            {
                bool valid = true;

                for (int x = 0; x < dr && valid; x ++)
                {
                    for (int y = 0; y < ur && valid; y ++)
                    {
                        if (mymap[i+x][j+y] < '0' or
                                mymap[i+x][j+y] > '3') valid = false;
                    }
                }

                if (valid)
                {
                    target_x = i;
                    target_y = j;
                    return true;
                }
            }
        }
        return false;
    }

}

bool findOpenocean(int begin_x, int begin_y, int dr, int ur, int &target_x, int &target_y)
{
    for (int i = begin_x; i >= 0 ; i --)
    {
        for (int j = begin_y ; j >= 0; j --)
        {
            bool valid = true;

            for (int x = 0; x < dr && valid; x ++)
            {
                for (int y = 0; y < ur && valid; y ++)
                {
                    if (mymap[i+x][j+y] != '~') valid = false;
                }
            }

            if (mymap[i+dr][j+ur] < '0'
                    or mymap[i+dr][j+ur] > '3')
                valid = false;

            if (valid)
            {
                target_x = i;
                target_y = j;
                return true;
            }
        }
    }
    return false;
}

//class

//Globalmanage
    void Globalmanage::TraverseFarmer()
    {
        for (tagFarmer &farmer : info.farmers)
        {
            SN_to_Farmer[farmer.SN] = farmer;
            if (farmer.FarmerSort == 0)
            {
                mymap[farmer.BlockDR][farmer.BlockUR] = '*'; //单位占用地图

                if (farmer.NowState == HUMAN_STATE_IDLE) idleFarmer.push_back(farmer);
                if (farmer.NowState == HUMAN_STATE_WORKING)
                {
                    int type = SN_to_Resource[farmer.WorkObjectSN].Type ;

                    if (type == RESOURCE_FISH ) fishingFarmer.push_back(farmer);
                    if (type == RESOURCE_STONE) choppingFarmer.push_back(farmer);
                    if (type == RESOURCE_ELEPHANT) huntingFarmer_ele.push_back(farmer);
                    if (type == RESOURCE_TREE) cuttingFarmer.push_back(farmer);
                    if (type == RESOURCE_GAZELLE) huntingFarmer_ga.push_back(farmer);

                    if (type == 0)
                    {
                        int type2 = SN_to_Building[farmer.WorkObjectSN].Type;
                        if (type2 == 0) buildingHouseFarmer.push_back(farmer);
                        if (type2 == 1) buildingGranaryFarmer.push_back(farmer);
                        if (type2 == 3) buildingStockFarmer.push_back(farmer);
                        if (type2 == 4) buildingFarmFarmer.push_back(farmer);
                        if (type2 == 5) buildingMarketFarmer.push_back(farmer);
                        if (type2 == 6) buildingArrowtowerFarmer.push_back(farmer);
                        if (type2 == 7) buildingArmycampFarmer.push_back(farmer);
                        if (type2 == 8) buildingStableFarmer.push_back(farmer);
                        if (type2 == 9) buildingRangeFarmer.push_back(farmer);
                        if (type2 == 10) buildingDockFarmer.push_back(farmer);

                    }

                    SN_isOccupied[farmer.WorkObjectSN] = true;
                }
            }
            if (farmer.FarmerSort == 2)
            {
                mymap[farmer.BlockDR][farmer.BlockUR] = '*'; //单位占用地图
                sailing.push_back(farmer);

                if (farmer.NowState == HUMAN_STATE_IDLE) idleSailing.push_back(farmer);
            }
            if (farmer.FarmerSort == 1)
            {
                mymap[farmer.BlockDR][farmer.BlockUR] = '*'; //单位占用地图
                boat.push_back(farmer);

                if (farmer.NowState == HUMAN_STATE_IDLE) idleBoat.push_back(farmer);
            }
        }
    }

    void Globalmanage::TraverseResource()
    {
        for (tagResource & resource : info.resources)
        {
            SN_to_Resource[resource.SN] = resource;
            SN_isOccupied[resource.SN] = false;

            //资源在地图上的信息
            if (resource.Type == RESOURCE_TREE)
            {
                mymap[resource.BlockDR][resource.BlockUR] = 'B';
                tree.push_back(resource);
            }
            if (resource.Type == RESOURCE_STONE)
            {
                mymap[resource.BlockDR][resource.BlockUR] = 'C';
                mymap[resource.BlockDR + 1][resource.BlockUR] = 'C';
                mymap[resource.BlockDR][resource.BlockUR + 1] = 'C';
                mymap[resource.BlockDR + 1][resource.BlockUR + 1] = 'C';
                stone.push_back(resource);
            }

            if (resource.Type == RESOURCE_GAZELLE)
            {
                mymap[resource.BlockDR][resource.BlockUR] = 'D';

                if (resource.Blood > 0) livega.push_back(resource);
                else deadga.push_back(resource);
            }
            if (resource.Type == RESOURCE_ELEPHANT)
            {
                mymap[resource.BlockDR][resource.BlockUR] = 'E';

                if (resource.Blood > 0) liveele.push_back(resource);
                else deadele.push_back(resource);
            }
            if (resource.Type == RESOURCE_GOLD)
            {
                mymap[resource.BlockDR][resource.BlockUR] = 'F';
            }
            if (resource.Type == RESOURCE_FISH)
            {
                mymap[resource.BlockDR][resource.BlockUR] = 'G';
                mymap[resource.BlockDR + 1][resource.BlockUR] = 'G';
                mymap[resource.BlockDR][resource.BlockUR + 1] = 'G';
                mymap[resource.BlockDR + 1][resource.BlockUR + 1] = 'G';
                fish.push_back(resource);
            }
        }
    }

    void Globalmanage::TraverseBuilding()
    {
        for (tagBuilding &building : info.buildings)
        {
            SN_to_Building[building.SN] = building;

            if (building.Type == BUILDING_HOME)
            {
                for (int x = 0; x < 2; x ++)
                {
                    for (int y = 0; y < 2; y ++)
                    {
                        mymap[building.BlockDR + x][building.BlockUR + y] = 'a' + BUILDING_HOME;
                    }
                }

                home.push_back(building);
            }

            if (building.Type == BUILDING_GRANARY)
            {
                for (int x = 0; x < 3; x ++)
                {
                    for (int y = 0; y < 3; y ++)
                    {
                        mymap[building.BlockDR + x][building.BlockUR + y] = 'a' + BUILDING_GRANARY;
                    }
                }

                granary.push_back(building);
            }
            if (building.Type == BUILDING_CENTER)
            {
                for (int x = 0; x < 3; x ++)
                {
                    for (int y = 0; y < 3; y ++)
                    {
                        mymap[building.BlockDR + x][building.BlockUR + y] = 'a' + BUILDING_CENTER;
                    }
                }

                mycenter.push_back(building);
            }

            if (building.Type == BUILDING_STOCK)
            {
                for (int x = 0; x < 3; x ++)
                {
                    for (int y = 0; y < 3; y ++)
                    {
                        mymap[building.BlockDR + x][building.BlockUR + y] = 'a' + BUILDING_STOCK;
                    }
                }

                stock.push_back(building);
            }
            if (building.Type == BUILDING_FARM)
            {
                for (int x = 0; x < 3; x ++)
                {
                    for (int y = 0; y < 3; y ++)
                    {
                        mymap[building.BlockDR + x][building.BlockUR + y] = 'a' + BUILDING_FARM;
                    }
                }

                farm.push_back(building);
            }

            if (building.Type == BUILDING_MARKET)
            {
                for (int x = 0; x < 3; x ++)
                {
                    for (int y = 0; y < 3; y ++)
                    {
                        mymap[building.BlockDR + x][building.BlockUR + y] = 'a' + BUILDING_MARKET;
                    }
                }

                market.push_back(building);
            }
            if (building.Type == BUILDING_ARROWTOWER)
            {
                for (int x = 0; x < 2; x ++)
                {
                    for (int y = 0; y < 2; y ++)
                    {
                        mymap[building.BlockDR + x][building.BlockUR + y] = 'a' + BUILDING_ARROWTOWER;
                    }
                }

                arrowtower.push_back(building);
            }

            if (building.Type == BUILDING_ARMYCAMP)
            {
                for (int x = 0; x < 3; x ++)
                {
                    for (int y = 0; y < 3; y ++)
                    {
                        mymap[building.BlockDR + x][building.BlockUR + y] = 'a' + BUILDING_ARMYCAMP;
                    }
                }

                armycamp.push_back(building);
            }
            if (building.Type == BUILDING_STABLE)
            {
                for (int x = 0; x < 3; x ++)
                {
                    for (int y = 0; y < 3; y ++)
                    {
                        mymap[building.BlockDR + x][building.BlockUR + y] = 'a' + BUILDING_STABLE;
                    }
                }

                stable.push_back(building);
            }

            if (building.Type == BUILDING_RANGE)
            {
                for (int x = 0; x < 3; x ++)
                {
                    for (int y = 0; y < 3; y ++)
                    {
                        mymap[building.BlockDR + x][building.BlockUR + y] = 'a' + BUILDING_RANGE;
                    }
                }

                range.push_back(building);
            }

            if (building.Type == BUILDING_DOCK)
            {
                for (int x = 0; x < 2; x ++)
                {
                    for (int y = 0; y < 2; y ++)
                    {
                        mymap[building.BlockDR + x][building.BlockUR + y] = 'a' + BUILDING_DOCK;
                    }
                }

                dock.push_back(building);
            }
        }
    }

    void Globalmanage::TraverseArmy()
    {
        for (tagArmy & army : info.armies)
        {
            if (army.Sort == AT_CLUBMAN)
            {
                clubman.push_back(army);
            }
            if (army.Sort == AT_SLINGER)
            {
                slinger.push_back(army);
            }
            if (army.Sort == AT_SWORDSMAN)
            {
                swordsman.push_back(army);
            }
            if (army.Sort == AT_BOWMAN)
            {
                bowman.push_back(army);
            }
            if (army.Sort == AT_SCOUT)
            {
                scout.push_back(army);
            }
            if (army.Sort == AT_SHIP)
            {
                ship.push_back(army);
                if (army.NowState == 0) idleship.push_back(army);
            }

        }
    }


//每帧
void UsrAI::processData()
{
    //初始化
    info = getInfo();
    setAll();//初始化地图

    switch(stage)
    {
    case -1:// 进行只需要一次的操作
    {
//        cheatAction();
        livega_num = livega.size();
        stage ++;
    }
    case 0:// 阶段1：基础建设与资源采集
    {
        //建筑：市中心1，仓库2，谷仓1，房子6
        //村民：陆地23，渔船5

        int idlefar_num = 0;
        int idlesai_num = 0;

        //若正在升级时代，则所有人砍树
        if (mycenter[0].Project == BUILDING_CENTER_UPGRADE && tree_flag == 0)
        {
            tree_flag = 1;
            DebugText(1);
            for (tagFarmer & farmer : info.farmers)
            {
                if (farmer.FarmerSort == 0)
                {
                    HumanAction(farmer.SN,
                                NearestResource(tree, farmer).SN);
                }
            }
        }

        //造房子(优先级高）
        int center_x = mycenter[0].BlockDR;
        int center_y = mycenter[0].BlockUR + 3;
        //如果房子小于6并且此帧有足够人空闲
        //若还有2人满员则新派1人造房
        if (home.size() < 6 && (info.Human_MaxNum - info.farmers.size() <= 2)
                && buildingHouseFarmer.size() < 1 && info.Wood >= 30)
        {
            if (idlefar_num < idleFarmer.size())
            {
                if (home_x == -1 && home_y == -1) //如果未初始化
                {
                    //预留农场位置； 预留八个房子(斜坡问题预留容错)
                    findOpenspace(2, center_x, center_y + 5, 8, 9, home_x, home_y);

                    for (int i = home_x; i < home_x + 7; i ++)
                    {
                        for (int j = home_y; j < home_y + 8; j ++)
                        {
                            mymap[i][j] = '*';
                        }
                    }
                }
                else
                {
                    home_y += 2;
                    if (home.size() == 4)
                    {
                        home_y -= 8;
                        home_x += 3;
                    }
                }

                HumanBuild(idleFarmer[idlefar_num].SN, BUILDING_HOME, home_x, home_y);
                idlefar_num ++;
            }
        }

        //造船坞,第一阶段造一个，优先等级低于造房
        if (dock.size() < 1 )
        {
            if (dock1_x == -1 && dock1_y == -1) //如果未初始化
            {
                tagResource nearestFish;
                nearestFish = NearestResource(fish, idleFarmer[idlefar_num]);
                findOpenocean(nearestFish.BlockDR, nearestFish.BlockUR,
                              2, 2, dock1_x, dock1_y);
            }
            if (idlefar_num < idleFarmer.size())
            {
                HumanBuild(idleFarmer[idlefar_num].SN, BUILDING_DOCK, dock1_x, dock1_y);
                idlefar_num ++;
            }

        }

        //砍木头
        //若存在空闲农民
        if (idlefar_num < idleFarmer.size())
        {
            //如果在木材需要较少阶段，派2人砍树
            if (info.farmers.size() < 6 && cuttingFarmer.size() < 2)
            {
                HumanAction(idleFarmer[idlefar_num].SN,
                            NearestResource(tree, idleFarmer[idlefar_num]).SN);
                idlefar_num ++;
            }
            if (info.farmers.size() >= 6 && cuttingFarmer.size() < 10)
            {
                HumanAction(idleFarmer[idlefar_num].SN,
                            NearestResource(tree, idleFarmer[idlefar_num]).SN);
                idlefar_num ++;
            }
        }

        //渔船捕鱼
        if (idlesai_num < idleSailing.size())
        {
            HumanAction(idleSailing[idlesai_num].SN,
                        NearestResource(fish, idleSailing[idlesai_num]).SN);
            idlesai_num ++;
        }


        //打猎//

        //大象(注意考虑数量问题，存在bug待优化)
        //这个在测试的时候很可能会遇到问题，谨慎。
        //如果农民足够且大象存在
        if (cuttingFarmer.size() > 4 && liveele.size())
        {
            //若存在大象血量健康，初始化狩猎
            if (liveele[0].Blood == BLOOD_ELEPHANT && liveele[1].Blood == BLOOD_ELEPHANT
                    && idlefar_num < idleFarmer.size() && huntingFarmer_ele.size() == 0
                    or
                liveele[0].Blood == BLOOD_ELEPHANT && liveele.size() == 1
                   && idlefar_num < idleFarmer.size() && huntingFarmer_ele.size() == 1 )
            {
                ele_blood = 45;
                hunter3 = idleFarmer[idlefar_num];
                idlefar_num ++;
                hunting_ele = liveele[0];

                HumanAction(hunter3.SN, hunting_ele.SN);
                eleflag = 1;
            }
            //若已经开始狩猎且大象受到攻击
            if (eleflag == 1 && SN_to_Resource[hunting_ele.SN].Blood < 45
                    && SN_to_Resource[hunting_ele.SN].Blood > 0)
            {
                if (SN_to_Resource[hunting_ele.SN].Blood < ele_blood
                        && SN_to_Farmer[hunter3.SN].NowState != HUMAN_STATE_WALKING)
                {
                    HumanMove(hunter3.SN, center_x * 35, center_y * 35);
                }
                else if (caldis(hunting_ele.BlockDR, hunting_ele.BlockUR,
                                SN_to_Farmer[hunter3.SN].BlockDR, SN_to_Farmer[hunter3.SN].BlockUR) > 6
                         && SN_to_Farmer[hunter3.SN].NowState != HUMAN_STATE_WORKING)
                {
                    HumanAction(hunter3.SN, hunting_ele.SN);
                }
                ele_blood = SN_to_Resource[hunting_ele.SN].Blood;

            }
//            cout << "flag:" << eleflag << endl;
//            cout << "blood:" << SN_to_Resource[hunting_ele.SN].Blood << endl;
//            cout <<"distance:" <<caldis(hunting_ele.BlockDR, hunting_ele.BlockUR,
//                           SN_to_Farmer[hunter3.SN].BlockDR, SN_to_Farmer[hunter3.SN].BlockUR) << endl;
//            cout << "state:" << SN_to_Farmer[hunter3.SN].NowState << endl;

        }
        //采集大象
        if (deadele.size() == 2 && huntingFarmer_ele.size() <= 12)
        {
            if (idlefar_num < idleFarmer.size())
            {
                HumanAction(idleFarmer[idlefar_num].SN, deadele[0].SN);
                idlefar_num ++;
            }
        }

        //瞪羚
        //若活瞪羚存在
        if (livega.size())
        {
            tagResource gazelle;
            //若无人在猎活瞪羚，初始化
            if (idleFarmer.size() - idlefar_num >= 2
                    && huntingFarmer_ga.size() == 0)
            {
                hunter1 = idleFarmer[idlefar_num];
                hunter2 = idleFarmer[idlefar_num + 1];

                idlefar_num += 2;

                gazelle = NearestResource(livega, hunter1);

                HumanAction(hunter1.SN, gazelle.SN);
                HumanAction(hunter2.SN, gazelle.SN);
            }

            if (livega_num > livega.size())
            {
                gazelle = NearestResource(livega, hunter1);

                HumanAction(hunter1.SN, gazelle.SN);
                HumanAction(hunter2.SN, gazelle.SN);

                livega_num --;
            }
        }
        else //若瞪羚全部死亡,修建一个仓库一个谷仓，并正常采集
        {
            if (stock.size() == 1 && idlefar_num < idleFarmer.size()
                    && info.Wood >= 120)
            {
                findOpenspace(2, deadga[0].BlockDR, deadga[0].BlockUR,
                                4, 4, stock_x, stock_y);
                HumanBuild(idleFarmer[idlefar_num].SN, BUILDING_STOCK,
                           stock_x, stock_y);
                idlefar_num ++;
            }
            if (granary.size() == 0 && idlefar_num < idleFarmer.size()
                    && info.Wood >= 120)
            {
                findOpenspace(2, deadga[0].BlockDR, deadga[0].BlockUR,
                                4, 4, granary_x, granary_y);
                HumanBuild(idleFarmer[idlefar_num].SN, BUILDING_GRANARY,
                           granary_x, granary_y);
                idlefar_num ++;
            }

            if (deadga.size() && idlefar_num < idleFarmer.size()
                    && huntingFarmer_ga.size() <= 12)
            {
                tagResource gazelle = NearestResource(deadga, idleFarmer[idlefar_num]);
                HumanAction(idleFarmer[idlefar_num].SN, gazelle.SN);
                idlefar_num ++;
            }
        }

        //市中心生产农民 （优先级高于升级）
        if (info.farmers.size() < info.Human_MaxNum &&
                mycenter[0].Project == 0 && info.Meat >= 50)
        {
            BuildingAction(mycenter[0].SN, BUILDING_CENTER_CREATEFARMER);
        }

        //船坞生产渔船
        if (info.Wood >= 60 && buildingDockFarmer.size() == 0
                && dock.size() == 1 && sailing.size() < 5)
        {
            BuildingAction(dock[0].SN, BUILDING_DOCK_CREATE_SAILING);
        }

        //市中心升级
        if(info.farmers.size() == info.Human_MaxNum
                && info.Meat >= 500 && home.size() == 6
                && sailing.size() == 5 && stock.size() == 2
                && granary.size() == 1)
        {
            BuildingAction(mycenter[0].SN, BUILDING_CENTER_UPGRADE);
        }

        //如果升级时代了，进入第二阶段
        if (info.civilizationStage == 2)
        {
            for (tagFarmer &farmer : info.farmers)
            {
                if (farmer.FarmerSort == 0)
                {
                    HumanMove(farmer.SN, farmer.DR , farmer.UR);
                }
            }
            stage ++;
        }

        //text


        break;
    }
    case 1:// 阶段2：海军建设与海域控制
    {
        //局部声明
        int idlesai_num = 0;
        int idlefar_num = 0;

        //首先进行上一阶段的查漏补缺
        //如果渔船没有捕完，建船坞,继续捕鱼
        if (fish.size())
        {
            if (dock.size() == 1)
            {
                if (dock2_x == -1 && dock2_y == -1 && idlefar_num < idleFarmer.size()) //如果未初始化
                {
                    tagResource nearestFish;
                    nearestFish = NearestResource(fish, idleFarmer[idlefar_num]);
                    findOpenocean(nearestFish.BlockDR, nearestFish.BlockUR,
                                  2, 2, dock2_x, dock2_y);
                    HumanBuild(idleFarmer[idlefar_num].SN, BUILDING_DOCK, dock2_x, dock2_y);
                    idlefar_num ++;
                }
            }
            if (idlesai_num < idleSailing.size())
            {
                HumanAction(idleSailing[idlesai_num].SN,
                            NearestResource(fish, idleSailing[idlesai_num]).SN);
                idlesai_num ++;
            }
        }
        else //如果补完了
        {
            for (tagFarmer &sail: sailing)
            {
                //渔船自我销毁
                HumanAction(sail.SN, sail.SN);
            }
        }
        //建造
        //造房子(感觉有一点问题，只造了七个房子）
        if (home.size() < 8 && buildingHouseFarmer.size() < 1 && info.Wood >= 30)
        {
            if (idlefar_num < idleFarmer.size())
            {
                home_y += 2;
                if (home.size() == 8)
                {
                    home_y -= 8;
                    home_x += 3;
                }

                HumanBuild(idleFarmer[idlefar_num].SN, BUILDING_HOME, home_x, home_y);
                idlefar_num ++;
            }

        }
        //市场
        if (market_x == -1 && market_y == -1)
        {
            if (idleFarmer.size() - idlefar_num >= 1)
            {
                findOpenspace(2, granary[0].BlockDR, granary[0].BlockUR + 3
                        , 5, 5, market_x, market_y);
                HumanBuild(idleFarmer[idlefar_num].SN, BUILDING_MARKET,
                           market_x + 1, market_y + 1);

                idlefar_num ++;
            }
            cout << mymap[market_x][market_y] << endl;
        }


        //农田（第一版：市中心）
        if (farm.size() < 8 && market.size() == 1 && buildingMarketFarmer.size() == 0)
        {
            if (idlefar_num < idleFarmer.size() && info.Wood > BUILD_FARM_WOOD)
            {
                if (farm.size() < 1)
                {
                    findOpenspace(2, mycenter[0].BlockDR, mycenter[0].BlockUR,
                            3, 4, farm_x, farm_y);
                    HumanBuild(idleFarmer[idlefar_num].SN, BUILDING_FARM, farm_x, farm_y + 1);
                    idlefar_num ++;
                    farm_x = -1, farm_y = -1;
                }
                else if (farm.size() < 2)
                {
                    findOpenspace(3, mycenter[0].BlockDR, mycenter[0].BlockUR,
                            3, 4, farm_x, farm_y);
                    HumanBuild(idleFarmer[idlefar_num].SN, BUILDING_FARM, farm_x, farm_y);
                    farm_x = -1, farm_y = -1;
                    idlefar_num ++;
                }
                else if (farm.size() < 5)
                {
                    if (farm_x == -1 && farm_y == -1)
                    {
                        findOpenspace(4, mycenter[0].BlockDR - 4, mycenter[0].BlockUR,
                                3, 9, farm_x, farm_y);
                    }
                    else farm_y += 3;
                    HumanBuild(idleFarmer[idlefar_num].SN, BUILDING_FARM, farm_x, farm_y);
                    idlefar_num ++;
                    if (farm.size() == 4) farm_x = -1, farm_y = -1;

                }
                else if (farm.size() < 8)
                {
                    if (farm_x == -1 && farm_y == -1)
                    {
                        findOpenspace(4, mycenter[0].BlockDR + 4, mycenter[0].BlockUR,
                                3, 9, farm_x, farm_y);
                    }
                    else farm_y += 3;
                    HumanBuild(idleFarmer[idlefar_num].SN, BUILDING_FARM, farm_x, farm_y);
                    idlefar_num ++;
                }
            }
        }

        //砍木头
        if (idlefar_num < idleFarmer.size() && cuttingFarmer.size() < 15)
        {
            HumanAction(idleFarmer[idlefar_num].SN,
                        NearestResource(tree, idleFarmer[idlefar_num]).SN);
            idlefar_num ++;
        }

        //船坞生产战船
        if (info.Wood >= 135 && buildingDockFarmer.size() == 0
                && dock.size() == 2 && ship.size() < 6)
        {
            if (dock[0].Project == 0)
            {
                BuildingAction(dock[0].SN, BUILDING_DOCK_CREATE_SHIP);
            }
            else if (dock[1].Project == 0)
            {
                BuildingAction(dock[1].SN, BUILDING_DOCK_CREATE_SHIP);
            }
        }


        //科技相关
        //农耕科技
        //市场已建好
        if (market.size() == 1)
        {
            //木头升级
            if (info.Meat >= 120 && info.Wood >= 75
                    && market[0].Project == 0 && buildingMarketFarmer.size() == 0)
            {
                BuildingAction(market[0].SN, BUILDING_MARKET_WOOD_UPGRADE);
            }
            //农田升级
            if (info.Meat >= 200 && info.Wood >= 50
                    && market[0].Project == 0 && buildingMarketFarmer.size() == 0)
            {
                BuildingAction(market[0].SN, BUILDING_MARKET_FARM_UPGRADE);
            }
        }
        //军事科技
        //金矿科技



        //Text
//        int c;
//        c = ship.size();
//        DebugText(c);


        break;
    }
    case 2:// 阶段3：登陆敌方大陆与军队建设
    {

        break;
    }
    case 3:// 阶段4：大规模资源采集
    {

        break;
    }
    }

}
